/**
 * 
 */
package signalprocessing;


import ikeuilib.JButtonWithMenu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

/**
 * 
 * YScaleを操作するコントロールの集まり
 * 
 * @author ikechan
 *
 */
public class PanelControlYScale extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int ScaleAuto_FITCenter=0;
	public static final int ScaleAuto_FITMaxMin=1;
	
	private JButtonWithMenu btnAmpAuto;
	private JButtonWithMenu btnSingle;
	private JButtonWithMenu btnTruncate;

	private PanelControlYScaleInterface Parent=null;
	
	private PanelControlYScaleConfiguration Conf = null;
	private ArrayList<String> MyFavotiteAmplitude = new ArrayList<String>(); 
	
	public PanelControlYScale(PanelControlYScaleConfiguration c){
		super();
		Conf = c; 
		//JPanel this = new JPanel();
		this.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), Conf.TitleBorder, TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		this.setBounds(7, 210, 189, 122);
		
		//panelControlPanelIntegration.add(this);
		
		this.setLayout(null);
		
		JButton btnSpanUp = new JButton("");
		btnSpanUp.setBounds(145, 20, 25, 21);
		this.add(btnSpanUp);
		btnSpanUp.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/wave22-small.png")));
		btnSpanUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.SpanU; // 200;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.SpanUKeyShift; // 1000;
				}
				Parent.ySpan(r);
			}
		});
		
		
		
		JButton btnSpanDown = new JButton("");
		btnSpanDown.setBounds(145, 54, 25, 21);
		this.add(btnSpanDown);
		btnSpanDown.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/wave22-big.png")));
		btnSpanDown.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r = Conf.SpanD; // 50;
				if((ActionEvent.SHIFT_MASK & e.getModifiers()) !=0){
					r = Conf.SpanDKeyShift; // 10;
				}
				Parent.ySpan(r);
			}
		});
		
		
		btnAmpAuto = new JButtonWithMenu();
		btnAmpAuto.setBounds(12, 20, 91, 26);
		this.add(btnAmpAuto);
		btnAmpAuto.setText("Auto");
		btnAmpAuto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(btnAmpAuto.isClickedDropDown()){
					JPopupMenu popup=new JPopupMenu();
					JMenuItem mi = new JMenuItem("*Zero centered");
					mi.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent arg0) {
							Parent.ySpanAuto(ScaleAuto_FITCenter);
						}

					});
					popup.add(mi);

					JMenuItem mi2 = new JMenuItem(" Fit max-min");
					mi2.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent arg0) {
							Parent.ySpanAuto(ScaleAuto_FITMaxMin);
						}

					});
					popup.add(mi2);
					popup.show(btnAmpAuto,0,btnAmpAuto.getHeight());
				}
				else{
					Parent.ySpanAuto(ScaleAuto_FITCenter);
				}

			}
		});

		JButton btnYShiftUp = new JButton("");
		btnYShiftUp.setBounds(117, 54, 25, 21);
		this.add(btnYShiftUp);
		btnYShiftUp.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/arrow_down.png")));
		btnYShiftUp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r=Conf.SlideP; //20;
				if((e.getModifiers()&ActionEvent.SHIFT_MASK) != 0){
					r = Conf.SlidePKeyShift; // 100;
				}
				Parent.ySpanShift(r);
			}
		});
		
		
		JButton btnYShiftDown = new JButton("");
		btnYShiftDown.setBounds(117, 20, 25, 21);
		this.add(btnYShiftDown);
		btnYShiftDown.setIcon(new ImageIcon(PanelControlYScale.class.getResource("/images/arrow_up.png")));
		btnYShiftDown.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int r=Conf.SlideN;// -20;
				if((e.getModifiers()&ActionEvent.SHIFT_MASK) != 0){
					r = Conf.SlideNKeyShift; // -100;
				}
				Parent.ySpanShift(r);
			}
		});
		
		
		if(Conf.SingleButton){
			btnSingle = new JButtonWithMenu();
			btnSingle.setBounds(12, 49, 91, 26);
			this.add(btnSingle);
			btnSingle.setText("Single");
			btnSingle.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(btnSingle.isClickedDropDown()){
						JPopupMenu popup=new JPopupMenu();
						JMenuItem mi = new JMenuItem(" Single");
						mi.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								Parent.ySpanSingle(0);
							}

						});
						popup.add(mi);

						JMenuItem mi3 = new JMenuItem("*Single by group");
						mi3.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								Parent.ySpanSingleGroup(0);
							}

						});
						popup.add(mi3);
						// TODO ＭＹ ＳＣＡＬＥを　実装する--------------------------------------------	
						{

						JMenuItem mmi2 = new JMenuItem("My scale...");
						mmi2.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/add.png")));
						mmi2.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								try{
									String s;
									if(MyFavotiteAmplitude.isEmpty()) s="";
									else s = MyFavotiteAmplitude.get(0);
									String imp = JOptionPane.showInputDialog(null,(Object)s,"Enter scale (max or min,max)",JOptionPane.PLAIN_MESSAGE);
									if(imp==null) return;
									if(!doScale(imp)){
										return;
									}
									for(int i=0;i<MyFavotiteAmplitude.size();i++){
										String ss = MyFavotiteAmplitude.get(i);
										if(ss.equals(imp)){
											MyFavotiteAmplitude.remove(i);
											break;
										}
									}
									MyFavotiteAmplitude.add(0,imp);
									if(MyFavotiteAmplitude.size()>5) MyFavotiteAmplitude.remove(5);
								} catch(NumberFormatException e){
									
								}
							}
						});
				        popup.add(mmi2);
				        for(int i=0;i<MyFavotiteAmplitude.size();i++){
				        	JMenuItem mmi30 = new JMenuItem(MyFavotiteAmplitude.get(i));
							mmi30.addActionListener(new ActionListener(){
								@Override
								public void actionPerformed(ActionEvent arg0) {
									try{
										String cmd = arg0.getActionCommand();
										doScale(cmd);
										//long val = Long.valueOf(cmd);
										//if(val<=0) return;
										//GraphicView.doSingleScale(val);
									}catch(NumberFormatException e){
										
									}
								}
							});		
							popup.add(mmi30);
				        }
						
						}
						//--------------------------------------------	
						
						popup.show(btnSingle,0,btnSingle.getHeight());
					}
					else{
						Parent.ySpanSingleGroup(0);
					}

				}
			});
		}
		
		if(Conf.TruncateButton){
			btnTruncate = new JButtonWithMenu();
			btnTruncate.setBounds(12, 85, 91, 26);
			this.add(btnTruncate);
			btnTruncate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(btnTruncate.isClickedDropDown()){
						JPopupMenu popup=new JPopupMenu();
						JMenuItem mi = new JMenuItem(" 1.0e-2");
						mi.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								if(Parent!=null){
									Parent.ySpanTruncate(0);
								}
							}

						});
						popup.add(mi);

						JMenuItem mi2 = new JMenuItem("*1.2e-3");
						mi2.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								// 普通は二桁、シフトで１ケタまるめ、
								Parent.ySpanTruncate(1);
							}

						});
						popup.add(mi2);

						JMenuItem mi3 = new JMenuItem(" 1.23e-4");
						mi3.addActionListener(new ActionListener(){
							@Override
							public void actionPerformed(ActionEvent arg0) {
								if(Parent!=null){
									Parent.ySpanTruncate(2);
								}
							}

						});
						popup.add(mi3);
						popup.show(btnTruncate,0,btnTruncate.getHeight());
					}
					else{
						if(Parent!=null){
							Parent.ySpanTruncate(1);
						}
					}
				}
			});
			btnTruncate.setText("Truncate");
		}

	}
	
	//----- 振幅指定を処理する。
	private boolean doScale(String imp){
		if(imp==null) return false;
		if(imp.length()==0) return false;
		String[] tok = imp.split(",");
		if(tok.length<0) return false;
		Double min,max;
		if(tok.length==1){
			max = Double.valueOf(tok[0]);
			min = -max;
		}
		else{
			max = Double.valueOf(tok[0]);
			min = Double.valueOf(tok[1]);
			if(max<min) {
				Double m = min;
				min = max;
				max = m;
			}
		}
		Parent.ySpan(max,min);
		return true;
	}//-----

	
	/**
	 * 
	 * @param p
	 */
	public void addParent(PanelControlYScaleInterface p){
		Parent = p;
	}

}
